//
//  JXHomeTableViewCell.swift
//  swift高仿微博
//
//  Created by peijiaxing on 16/3/2.
//  Copyright © 2016年 peijiaxing. All rights reserved.
//

import UIKit
import SnapKit

class JXHomeTableViewCell: UITableViewCell {
    
    var constant: Constraint?

    var statusViewModel: JXStatusViewModel?{
        
        didSet{
            orginalView.statusViewModel = statusViewModel
            
            footerView.footerViewModel = statusViewModel
            
            // cell有重用导致了约束加重,
            // 先清空约束
            constant?.uninstall()
            
            if statusViewModel?.statusModel?.retweeted_status == nil{
                // 没有转发微博
                
                /*
                
                修改这个约束 make.top.equalTo(retweetView.snp_bottom)
                隐藏转发微博视图
                */
                retweetView.hidden = true
                // 更新一下顶部约束
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constant = make.top.equalTo(orginalView.snp_bottom).constraint
                })
                
            } else{
                // 有转发微博
                /*
                修改这个约束 make.top.equalTo(retweetView.snp_bottom)
                隐藏转发微博视图
                传值
                */
                retweetView.hidden = false
                
                retweetView.retweetViewModel = statusViewModel
                
                // 更新一下顶部约束
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constant = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }
        }
        
    }

    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        
        // 添加到视图里
        contentView.addSubview(orginalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        
        // 添加约束 左 上 右 下
        orginalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            //            make.bottom.equalTo(contentView.snp_bottom)
        }
        
        /*
        有转发微博,也可能没有转发微博
        1.处理转发微博的时候,先按照都有转发微博
        */
        /*
        针对特例进行分析
        1. 当没有转发微博的时候,我需要改什么东西?
        make.top.equalTo(retweetView.snp_bottom)
        隐藏转发微博视图
        
        */
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            
            // 注意
            make.top.equalTo(orginalView.snp_bottom)
            
        }
        
        // 添加 左, 上(orginalView),右边,下边
        footerView.snp_makeConstraints { (make) -> Void in
            
            self.constant =  make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.bottom.equalTo(contentView.snp_bottom)
            
            //设置高度
            make.height.equalTo(40)
        }

        
    }
    // 把原创微博添加到视图里
    private lazy var orginalView: JXOrginal = JXOrginal()
    // 底部评论按钮
    private lazy var footerView: JXFooterView = JXFooterView()
    // 转发微博
    private lazy var retweetView: JXRetweetView = JXRetweetView()

    
}
